﻿/*** run functions ***/
function fr3_proceed($) {
  /* input field watermark */
  function formWatermark(c){
    var notselectors = [
      'input:file',
      'input:checkbox',
      'input:radio',
      'input:submit',
      '.watermproc'
    ];
    setTimeout(function(){
      $(c).not(notselectors.join(', ')).watermark().addClass('watermproc');
    }, 200);
  };

  function formWatermarkInit(){
    formWatermark('.form-item input');
    formWatermark('.form-item textarea');
  };

  /* Select regions */
  var stopDropDown = null;
  function fr3SelectRegions(c, t){
    $(c).each(function(){
      var $this = $(this);
      var element = $this.find('.label');
      var dropDown = $('#fr3-regions-map');

      element
        .bind('mouseover', function(){
          clearTimeout(stopDropDown);stopDropDown = null;

          element = $(this);
          element.addClass('label-over');

          var elementPosition = $this.offset();
          var dropDownHeight = dropDown.height();

          if(element.parents('#fr3-footer').length){
            dropDown
              .css({ top: elementPosition.top - dropDownHeight })
              .addClass('fr3-regions-map-footer');
          }else{
            dropDown
              .css({ top: elementPosition.top + 40 });
          };
          dropDown
            .css({ left: elementPosition.left });
        })
        .bind('mouseout', function(){
          element = $(this);

          if (stopDropDown == null){
            stopDropDown = setTimeout(function(){ closeDropDown(); }, t);
          };
        });

      dropDown
        .bind('mouseover', function(){
          clearTimeout(stopDropDown);stopDropDown = null;
        })
        .bind('mouseout', function(){
          if (stopDropDown == null){
            stopDropDown = setTimeout(function(){ closeDropDown(); }, t);
          };
        });
    });

    function closeDropDown(){
      var element = $(c).find('.label');
      var dropDown = $('#fr3-regions-map');

      element.removeClass('label-over');
      dropDown.attr('style', ' ').attr('class', ' ');
    };
  };

  function fr3SelectRegionsInit(){
    fr3SelectRegions('.fr3-select-region', 150);
  };

  /* Map hilight */
  function mapHilight(c, linksBlock){
    var $block = $(c).not('.maphilight-processed');

    var default_options = {
      fillOpacity: 1,
      fillColor: '3D8AC5',
      stroke: true,
      strokeColor: 'FFFFFF',
      strokeOpacity: 0.5,
      strokeWidth: 2
    };
    var selected_options = {
      alwaysOn: true
    };
    var disable_options = {
      fillOpacity: 0,
      stroke: false
    };

    $block.addClass('maphilight-processed');
    var $img = $block.find('img');
    var $areas = getMapAreas($img);
    var $links = $block.find(linksBlock).find('a');

    if($areas){
      $areas.each(function(){
        var $this = $(this);
        var index = $areas.index(this);
        $this.not('.selected')
          .bind('mouseover', function(){ $links.filter("[rel='"+index+"']").addClass('over'); })
          .bind('mouseout', function(){ $links.filter("[rel='"+index+"']").removeClass('over'); });

        if(!$links.filter("[rel='"+index+"']").length){
          $this
            .addClass('disable')
            .data('maphilight', disable_options);
        };
      });
    };

    $links.each(function(){
      var $link = $(this);
      var index = $link.attr('rel');
      if($link.hasClass('selected')){
        $link.attr('href', 'javascript:void(0)');
        $areas.eq(index)
          .addClass('selected')
          .attr('href', 'javascript:void(0)')
          .data('maphilight', selected_options);
      };
      $link.not('.selected')
        .bind('mouseover', function(){ $areas.eq(index).mouseover(); })
        .bind('mouseout', function(){ $areas.eq(index).mouseout(); });
    });

    $img.maphilight(default_options);
  };

  function getMapAreas($image) {
    var usemap = $image.attr('usemap');
    if (typeof(usemap) == 'undefined'){ return false; };
    var matches = usemap.match(/#.+$/);
    if (matches.length != 1){ return false; };
    usemap = matches[0];
    return $(usemap).find('area');
  };

  function mapHilightInit(){
    mapHilight('#fr3-regions-map-hilight', '.block-regions-map-links');
  };

  /* functions for IE */
  function functionsForIe(){
    /* submit buttons for IE8 */
    if ($.browser.msie && $.browser.version.substr(0,1)==8){
      $('.form-submit').mousedown(function(){
          $(this).removeClass('form-submit-unclick').addClass('form-submit-click');
          $(this).mouseout(function(){
            $(this).removeClass('form-submit-click').addClass('form-submit-unclick');
          });
      }).mouseup(function(){
          $(this).removeClass('form-submit-click').addClass('form-submit-unclick');
      });
    };

    /* position of login area for IE6 */
    if ($.browser.msie && $.browser.version.substr(0,1)<7){
      $('body').addClass('site-body-IE6');

      var loginArea = $('.fr3-login-area');
      loginArea.width($('body').width());
      loginArea.css({
        marginLeft: - (parseInt($('body').width())/2) + 'px'
      });
      $(window).resize(function(){
        loginArea.width($('body').width());
        loginArea.css({
          marginLeft: - (parseInt($('body').width())/2) + 'px'
        });
      });
    };
  };

  formWatermarkInit();
  mapHilightInit();
  fr3SelectRegionsInit();

}

(function fr3_init($) {
  var jQueryScriptOutputted = false;
  if (typeof(jQuery) == 'undefined') {
    if (! jQueryScriptOutputted) {
      jQueryScriptOutputted = true;
      document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\"></scr" + "ipt>");
      setTimeout(function() {
        fr3_init();
      }, 250);
    }
  }
  else {

    $(".fr3-region-element").ready(function() {
      var watermarkUrl = "http://regions.france3.fr/sites/regions_france3/themes/fr3r/scripts/watermark.js";
      var maphilightUrl = "http://regions.france3.fr/sites/regions_france3/themes/fr3r/scripts/maphilight.js";

      if (typeof($(".fr3-region-element").watermark) != "function" && typeof($(".fr3-region-element").maphilight) != "function") {

        $.getScript(watermarkUrl, function (data, textStatus, jqxhr) {

          $.getScript(maphilightUrl, function (data, textStatus, jqxhr) {
            fr3_proceed(jQuery);
          });
        });
      }
      else if (typeof($(".fr3-region-element").watermark) != "function") {
        $.getScript(watermarkUrl, function (data, textStatus, jqxhr) {
          fr3_proceed(jQuery);
        });
      }
      else if (typeof($(".fr3-region-element").maphilight) != "function") {
        $.getScript(maphilightUrl, function (data, textStatus, jqxhr) {
          fr3_proceed(jQuery);
        });
      }
      else {
        fr3_proceed(jQuery);
      }
    });
  }

}(jQuery));
